﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint.Utilities;
using System.Threading;

namespace FTI.SP.LADM.EnhancedContentQueryWebPart
{
    internal static class BuilderButton
    {
        // Methods
        private static bool BrowserIsFF(HttpBrowserCapabilities browser)
        {
            return (browser.Browser.ToUpperInvariant() == "FIREFOX");
        }

        private static bool BrowserIsFF20OrAbove(HttpBrowserCapabilities browser)
        {
            return (BrowserIsFF(browser) && (browser.MajorVersion >= 2));
        }

        private static bool BrowserIsIE(HttpBrowserCapabilities browser)
        {
            return (!PlatformIsMac(browser) && (browser.Browser == "IE"));
        }

        private static bool BrowserIsIE60OrAbove(HttpBrowserCapabilities browser)
        {
            return (BrowserIsIE(browser) && (browser.MajorVersion >= 6));
        }

        internal static bool DynamicBuilderButtons(HttpBrowserCapabilities browser)
        {
            if (!BrowserIsIE60OrAbove(browser))
            {
                return BrowserIsFF20OrAbove(browser);
            }
            return true;
        }

        public static string GetBuilderButtonMarkup(bool dynamic, string wpClientId, string controlId, HttpContext context)
        {
            string scriptLiteralToEncode = "/_layouts/zoombldr.aspx";
            string str2 = "dialogHeight:570px;dialogWidth:430px;help:no;status:no;resizable:yes";
            string str3 = string.Empty;
            if (dynamic)
            {
                if (BrowserIsIE60OrAbove(context.Request.Browser))
                {
                    str3 = "onfocusout=\"this.style.display='none';\"";
                }
                else
                {
                    str3 = "onblur=\"this.style.display='none';\"";
                }
            }
            else
            {
                str3 = "style=\"display: inline\"";
            }
            return string.Format("&nbsp;<input type=\"button\" class=\"ms-PropGridBuilderButton\" value=\"...\" id=\"{0}\" title=\"Click to use builder\" tabindex=\"0\" onclick=\"MSOPGrid_doBuilder('{1}?culture={2}', {3}, '{4}');\" {5} />", new object[] { GetBuilderId(wpClientId, controlId), SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode), Thread.CurrentThread.CurrentUICulture, SPHttpUtility.EcmaScriptStringLiteralEncode(GetBuilderEditorId(wpClientId, controlId)), str2, str3 });
        }

        public static string GetBuilderEditorId(string wpClientId, string controlId)
        {
            return (wpClientId + '_' + controlId);
        }

        public static string GetBuilderId(string wpClientId, string controlId)
        {
            return string.Format("{0}_Builder", GetBuilderEditorId(wpClientId, controlId));
        }

        internal static string GetBuilderVisibilityFunctionCall(string wpClientId, string controlId)
        {
            return string.Format("MSOPGrid_BuilderVisible({0})", GetBuilderId(wpClientId, controlId));
        }

        internal static string GetDeactivateBuilderFunctionCall(string wpClientId, string controlId)
        {
            return string.Format("MSOTlPn_prevBuilder={0};", GetBuilderId(wpClientId, controlId));
        }

        private static bool PlatformIsMac(HttpBrowserCapabilities browser)
        {
            return (browser.Platform == "MacPPC");
        }

    }
}
